package com.qu.visitmodel.utils;

import android.graphics.Bitmap;
import android.media.MediaMetadataRetriever;
import android.util.Log;

import java.io.File;

import static com.qu.visitmodel.service.RecordService.TAG;

public class VideoThumbnail {

    public static Bitmap createVideoThumbnail(File videoFile, int position) {
        Bitmap bitmap = null;
        try {
            MediaMetadataRetriever retriever = new MediaMetadataRetriever();
            retriever.setDataSource(videoFile.getAbsolutePath());
            String hasVideo = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_HAS_VIDEO);
            Log.e(TAG, "hasVideo:"+hasVideo);
            if(hasVideo == null)
            {
                videoFile.delete();
                return null;
            }

            String duration = retriever.extractMetadata(MediaMetadataRetriever.METADATA_KEY_DURATION);
            int attime = Integer.parseInt(duration)/2;
            if(position > 0)
            {
                attime = position;
            }
            bitmap = retriever.getFrameAtTime(attime*1000, MediaMetadataRetriever.OPTION_CLOSEST_SYNC);;

            retriever.release();
            retriever = null;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bitmap;
    }

}